<template>
	<scroll-view scroll-y="true" class="page">
		<u-navbar :placeholder="true" title="发表动态" @leftClick="$utils.back()" border>

		</u-navbar>
		<view>

			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					标题：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u-textarea autoHeight  maxlength='999'   placeholder="请输入内容" border="none" v-model="textarea" />
				</view>
			</view>
			<view style="align-items: center;" class="  borderBottom flex pb20 pt20">
				<view class="mr20 ">
					媒体内容：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<view v-if="imageUrl.length==0&&!video.url" class="mt20 upload flex" @click="openSelect">
						<u-icon name="camera-fill" color="#D3D4D6" size="50"></u-icon>
					</view>
					<view :class="{mt20:imageUrl.length!=0}" v-if="imageShow">
						<imageUpload :imageShow='imageShow' @changeImageList='changeImageList' :imageUrl='imageUrl'
							@close='imageShow=null' :conquer_id='conquer_id' />
					</view>
					<view :class="{mt20:video.url}" v-if="videoShow">
						<videoUpload @changeVideo='changeVideo' @close='videoShow=false' />
					</view>
				</view>
			</view>
			
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					积分项：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<view class="button" @click="rewardsShow=true">
						添加积分项
					</view>
				</view>
			</view>
			<view class="rewardsBox" v-if="rewards.length">
				<view>
					<view>
						标题
					</view>
					<view>
						积分
					</view>
					<view>
						操作
					</view>
				</view>
				<view v-for="(item,index) in rewards" :key="index">
					<view>
						{{item.title}}
					</view>
					<view>
						<u-number-box v-model="item.point" :min='0' :max='item.max'>
							<view slot="minus" class="minus">
								<u-icon name="minus" size="12"></u-icon>
							</view>
							<view slot="plus" class="plus">
								<u-icon name="plus" color="#FFFFFF" size="12"></u-icon>
							</view>
						</u-number-box>
					</view>
					<view @click="delRewards(index)">
						<u-icon name='trash' />
					</view>
				</view>
			</view>
			<u-popup :show="showSelect" :round="10" @close="showSelect=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						选择方式
					</view>
					<u-icon name="close" @click="showSelect=false"></u-icon>
				</view>
				<view style="height: 20vh;">
					<u-grid :border="false" @click="clickGrid">
						<u-grid-item v-for="(baseListItem,baseListIndex) in baseList" :key="baseListIndex">
							<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.name" :size="22"></u-icon>
							<text class="grid-text">{{baseListItem.title}}</text>
						</u-grid-item>
					</u-grid>
				</view>
			</u-popup>
			<u-popup :show="rewardsShow" :round="10" @close="rewardsShow=false">
				<view class="popupTitle">
					<u-icon name="close" @click="rewardsShow=false"></u-icon>
					<view>
						积分项
					</view>
					<view @click="jumpPoint">
						添加
					</view>
				</view>
				<view style="height: 40vh;">
					<z-paging ref="paging" :refresher-enabled='false' style="height: 100%;" :fixed="false"
						v-model="localdata" :auto="false">
						<u-checkbox-group v-model="checkboxValue1" placement="column">
							<view v-for="(item, index) in localdata" :key="index" class="checkbox ">
								<u-checkbox :name="item.id" />
								<view @click="choose(item.id)">
									<u-text :text='item.title' lines='1' />
								</view>
								<view>
									{{item.point}}分
								</view>
							</view>
						</u-checkbox-group>
					</z-paging>
				</view>
			</u-popup>
		</view>
		<view class="bottom viewWidth">
			<u-button :disabled="disabled" @click="pulish">发表</u-button>
		</view>
		<view class="imageBox" v-if="show" @click="jumpWish">
			<image src="@/static/image/2acc10b70bf5729237957753b9a68cd.png" />
			<view>
				恭喜您，心愿已经达成
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import rulerComponent from "@/components/sapling-vue-scale/sapling-vue-scale";
	import imageUpload from '@/components/imageUpload';
	import videoUpload from '@/components/videoUpload';
	export default {
		components: {
			imageUpload,
			videoUpload,
			rulerComponent
		},
		data() {
			return {
				show:false,
				checkboxValue1: [],
				rewardsShow: false,
				localdata: [],
				wish_id: null,
				showSelect: false,
				select: null,
				rewards: [],
				imageShow: null,
				videoShow: false,
				baseList: [{
						name: 'photo',
						title: '本地图片'
					},
					{
						name: 'play-right',
						title: '本地视频'
					},
					{
						name: 'chat',
						title: '素材聊天'
					},
				],
				imageUrl: [],
				video: {

				},
				textarea: null,
				disabled: false,
				loading: true,
			}
		},
		watch: {
			checkboxValue1() {
				let arr = this.localdata.filter(item => this.checkboxValue1.includes(item.id))
				const ids = this.rewards.map(item => item.id)
				if (arr.length > this.rewards.length) {
					arr = arr.filter(item => !ids.includes(item.id))
					this.rewards.push(...arr)
				} else {
					this.rewards = this.rewards.filter(item => this.checkboxValue1.includes(item.id))
				}

			}
		},
		onLoad(op) {
			this.wish_id = op.id
			this.$api.My.Point.List({page:1,size:100}).then(res => {
				uni.hideLoading()
				this.$refs.paging.completeByNoMore(res.data.points.data.map(item => {
					return {
						...item,
						max: item.point
					}
				}),true)
				this.loading = false
			}).catch(err => {
				uni.hideLoading()
				uni.$u.toast(err.data.message)
			})
		},
		methods: {
			jumpWish(){
				uni.reLaunch({
					url: '/wish/detail?id=' + this.wish_id
				})
			},
			jumpPoint(){
				this.$utils.navigateTo({
					url:'/My/point'
				})
			},
			choose(id) {
				if (this.checkboxValue1.includes(id)) {
					this.checkboxValue1 = this.checkboxValue1.filter(item => item != id)
				} else {
					this.checkboxValue1.push(id)
				}
			},
			delRewards(index) {
				this.rewards = this.rewards.filter((item, itemIndex) => index != itemIndex)
				const ids = this.rewards.map(item => item.id)
				this.checkboxValue1 = this.checkboxValue1.filter(item => ids.includes(item))
			},

			changeVideo(e) {
				console.log(e)
				this.video = e
			},
			openSelect() {
				this.showSelect = true
			},
			clickGrid(e) {
				if (e == 0) {
					this.imageShow = 'photo'
					this.showSelect = false
				} else if (e == 1) {
					this.videoShow = true
					this.showSelect = false
				} else {
					this.imageShow = 'chat'
					this.showSelect = false
				}
			},

			changeImageList(e) {
				this.imageUrl = e
			},
			pulish() {
				if (!this.imageUrl.every(item => item.upload) && this.imageUrl.length) {
					uni.$u.toast('正在上传中')
					return
				}
				if (!this.video.upload && this.video.url) {
					uni.$u.toast('正在上传中')
					return
				}

				if (!this.imageUrl.length && !this.video.id) {
					uni.$u.toast('必须上传图片视频')
					return
				}
				if (!this.textarea) {
					uni.$u.toast('请输入内容')
					return
				}
				if (this.textarea.length < 6) {
					uni.$u.toast('内容长度不能小于6个字符')
					return
				}
				this.Add()

			},
			Add() {
				this.disabled = true
				let obj = {
					wish_id: this.wish_id,
					points: this.rewards.map(item => {
						return {
							reward_id: item.id,
							value: item.point,
							title: item.title
						}
					}),
					title: this.textarea,
					images: this.imageUrl.map(item => {
						return {
							url: item.url,
							small_url: item.small_url,
							title: item.title
						}
					}),
					video_ids: this.video.id ? [this.video.id] : [],
				}
				this.$api.Wish.Active.Add(obj).then(res => {
					uni.$u.toast('新增动态成功')
					if(res.data.isFulfilled){
						this.show=true
					}else{
						setTimeout(() => {
							uni.reLaunch({
								url: '/wish/detail?id=' + this.wish_id
							})
						}, 500)
					}
				}).catch(err => {
					this.disabled = false
					console.log(err)
					uni.$u.toast(err.data.message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-textarea {
		padding: 0 !important;
	text-align: right;
	}
	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.page {
		display: flex;
		height: calc(100vh - constant(safe-area-inset-bottom) + 10rpx);
		height: calc(100vh - env(safe-area-inset-bottom) + 10rpx);
		flex-direction: column;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.borderBottom {
		border-bottom: 1rpx solid #f1f1f1;
	}






	.flex {
		display: flex;
	}

	.pb20 {
		padding-bottom: 20rpx;
	}

	.pt20 {
		padding-top: 20rpx;
	}

.upload {
		border-radius: 8rpx;
		background-color: #f5f5f5;
		width: 100rpx;
		height: 100rpx;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	::v-deep .u-border {
		border-color: #fff !important;
	}

	::v-deep .u-textarea {
		padding: 0 !important;
	}

	.selectItem {
		font-size: 30rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.selectItem:nth-child(1) {
		border-bottom: 1rpx solid #f1f1f1;
	}

	.userBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx 0;

		view:nth-child(2) {
			flex: 0.98;
		}
	}

	.bottom {
		margin-top: 20rpx;
		width: calc(100% - 40rpx);
	}

	.rewardsBox {
		view {
			display: flex;
			justify-content: center;
		}
	
		>view {
			display: flex;
			align-items: center;
			padding: 10rpx 0;
			border-bottom: 1rpx solid #f5f5f5;
			>view:nth-child(1) {
				justify-content: flex-start !important;
				padding: 0 10rpx;
				flex: 0.4;
			}
	
			>view:nth-child(2) {
				flex: 0.4;
			}
	
			>view:nth-child(3) {
				flex: 0.2;
			}
		}
	
		>view:nth-child(1) {
			background-color: #f5f5f5;
			border-radius: 8rpx;
		}
	}

	.button {
		border-radius: 100rpx;
		border: 1rpx solid #bbb;
		padding: 5rpx 10rpx;
		font-size: 24rpx;
	}

	.checkbox {
		display: flex;
		padding: 10rpx ;
		align-items: center;
		margin:10rpx 20rpx;
		border-radius: 8rpx;
		>view:nth-child(2) {
			flex: 1;
			margin: 0 20rpx;
		}
	}
	.imageBox{
		z-index: 3;
		width: 100vw;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		image{
			width: 40vw;
			height: 20vh;
		}
		>view{
			margin-top: 30rpx;
			background-color: #E6364B;
			padding: 5rpx 10rpx;
			border-radius: 100rpx;
			color:#fff;
		}
	}
</style>